package com.edgar.paysdk

import android.app.Activity
import android.support.v4.util.Pools
import java.lang.ref.WeakReference
import java.util.*

/**
 * Created by edgar on 18-1-20.
 */
class PayReq private constructor(){

    private val mPayParams : MutableMap<String,String> = HashMap()
    private var mActivityRef:WeakReference<Activity>? = null

    companion object {
        @JvmStatic val KEY_SIGN = "sign"
        @JvmStatic val KEY_APP_ID = "appId"
        @JvmStatic val KEY_SIGN_TYPE = "signType"
        @JvmStatic val KEY_ORDER_INFO = "orderInfo"
        @JvmStatic val KEY_PACKAGE_VALUE = "packageValue"
        @JvmStatic val KEY_NONCE_STR = "nonceStr"
        @JvmStatic val KEY_PARTNER_ID = "partnerId"
        @JvmStatic val KEY_PREPAY_ID = "prepayId"
        @JvmStatic val KEY_TIME_STAMP = "timeStamp"
        @JvmStatic val sPayReqPool : Pools.SynchronizedPool<PayReq> = Pools.SynchronizedPool(3)

        @JvmStatic fun obtain(activity: Activity,sign: String):PayReq{
            var payReq = sPayReqPool.acquire()
            if (payReq == null){
                payReq = PayReq()
            }
            payReq.setSign(sign)
            payReq.setActivity(activity)
            return payReq
        }
    }

    fun release(){
        sPayReqPool.release(this)
        mPayParams.clear()
        mActivityRef?.clear()
        mActivityRef = null
    }

    private fun setActivity(activity: Activity?){
        mActivityRef = WeakReference<Activity>(activity)
    }

    fun getActivity(): Activity? = mActivityRef?.get()

    private fun setSign(sign:String){
        mPayParams.put(KEY_SIGN,sign)
    }

    fun getSign():String? = mPayParams[KEY_SIGN]

    fun setSignType(signType:String):PayReq{
        mPayParams.put(KEY_SIGN_TYPE,signType)
        return this
    }
    fun getSignType():String? = mPayParams[KEY_SIGN_TYPE]

    fun setOrderInfo(orderInfo:String):PayReq{
        mPayParams.put(KEY_ORDER_INFO,orderInfo)
        return this
    }
    fun getOrderInfo():String? = mPayParams[KEY_ORDER_INFO]

    fun setPackageValue(packageValue:String):PayReq{
        mPayParams.put(KEY_PACKAGE_VALUE,packageValue)
        return this
    }
    fun getPackageValue():String? = mPayParams[KEY_PACKAGE_VALUE]

    fun setNonceStr(nonceStr:String):PayReq{
        mPayParams.put(KEY_NONCE_STR,nonceStr)
        return this
    }
    fun getNonceStr():String? = mPayParams[KEY_NONCE_STR]

    fun setPartnerId(partnerId:String):PayReq{
        mPayParams.put(KEY_PARTNER_ID,partnerId)
        return this
    }
    fun getPartnerId():String? = mPayParams[KEY_PARTNER_ID]

    fun setPrePayId(prepayId:String):PayReq{
        mPayParams.put(KEY_PREPAY_ID,prepayId)
        return this
    }
    fun getPrePayId():String? = mPayParams[KEY_PREPAY_ID]

    fun setTimeStamp(timeStamp:String):PayReq{
        mPayParams.put(KEY_TIME_STAMP,timeStamp)
        return this
    }
    fun getTimeStamp():String? = mPayParams[KEY_TIME_STAMP]

    fun setAppId(appId:String):PayReq{
        mPayParams.put(KEY_APP_ID,appId)
        return this
    }
    fun getAppId():String? = mPayParams[KEY_APP_ID]

    override fun toString(): String {
        val stringBuilder = StringBuilder()
        mPayParams.mapValues { entry ->
            stringBuilder.append(entry.key)
                    .append("=")
                    .append(entry.value)
                    .append("&")
        }
        return stringBuilder.delete(stringBuilder.length-1,stringBuilder.length).toString()
    }
}